package requestx

import (
	"context"
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
	"xialou_platform/app/errorx"
	"xialou_platform/app/jwtx"
)

func FetchUserIdByJwt(ctx context.Context) (int64, error) {
	value := ctx.Value("claim")
	if value != nil {
		switch value.(type) {
		case jwtx.MyClaims:
			return value.(jwtx.MyClaims).Uid, nil
		case int64:
			return value.(int64), nil
		case interface{}:
			uid, err := value.(json.Number).Int64()
			if err != nil || uid == 0 {
				logx.WithContext(ctx).Infof("【valid auth token】: User JWT token illegal")
				return 0, errorx.NewMsgCodeError(errorx.UnLoginCode, "请重新登录")
			}
			return uid, nil
		}
	}
	return 0, errorx.NewMsgCodeError(errorx.UnLoginCode, "请重新登录")
}
func FetchIdByJwt(ctx context.Context) (int64, error) {
	value := ctx.Value("claim")
	if value != nil {
		switch value.(type) {
		case jwtx.MyClaims:
			return value.(jwtx.MyClaims).Id, nil
		case int64:
			return value.(int64), nil
		case interface{}:
			uid, err := value.(json.Number).Int64()
			if err != nil || uid == 0 {
				logx.WithContext(ctx).Infof("【valid auth token】: User JWT token illegal")
				return 0, errorx.NewMsgCodeError(errorx.UnLoginCode, "请重新登录")
			}
			return uid, nil
		}
	}
	return 0, errorx.NewMsgCodeError(errorx.UnLoginCode, "请重新登录")
}
